import { _decorator } from "cc";
import BaseComp from "../BaseComp";
import { Label } from "cc";
import { Sprite } from "cc";
import { tween } from "cc";
import { UIOpacity } from "cc";
import { UITransform } from "cc";

const { ccclass, property } = _decorator;

@ccclass
export default class CommonToast extends BaseComp {

    private title: Label = null;
    private bg: Sprite = null;

    __preload() {
        this.openFilter = true;
        super.__preload();
    }

    onLoad() {

    }

    /**
     * 
     * 展示toast
     * @param  {number} time toast展示的时间 以秒为单位
     * @param  {string} message toast展示的内容
     * 
     */
    toast(time: number, message: string) {
        // 透明度由0 -> 255 在停顿2秒 然后 255 -> 0
        this.title.string = message;
        const opacityComp = this.node.getComponent(UIOpacity);
        opacityComp.opacity = 255;
        this.scheduleOnce(() => {
            const titleUIComponent = this.title.node.getComponent(UITransform);
            const w = titleUIComponent.width;
            console.log("w is ", w);
            const bgTransform = this.bg.node.getComponent(UITransform);
            bgTransform.width = w + 80;

            tween(opacityComp).to(0.2, { opacity: 255 }).delay(time).to(0.2, { opacity: 0 }).call(() => {
                console.log("toast 完毕");
                this.node.destroy();
            }).start();
        }, 0.02);
    }

    start() {

    }

    // update (dt) {}
}
